package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guaike.core.entity.enums.EnableStatusEnum;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.drg.center.entity.SysIndexPO;
import com.drg.center.mapper.SysIndexMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: zhangxilin
 * @Date: 2023/10/30
 */
@Repository
public class SysIndexDAO extends BaseDAO<SysIndexMapper, SysIndexPO> {

    public List<SysIndexPO> findList(){
        LambdaQueryWrapper<SysIndexPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysIndexPO::getStatus, EnableStatusEnum.ENABLE.getCode());
        lambdaQueryWrapper.orderByAsc(SysIndexPO::getSortIndex);
        return getBaseMapper().selectList(lambdaQueryWrapper);
    }

    public SysIndexPO findOne(String sysCode){
        LambdaQueryWrapper<SysIndexPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysIndexPO::getStatus, EnableStatusEnum.ENABLE.getCode());
        lambdaQueryWrapper.eq(SysIndexPO::getSysCode,sysCode);
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }

    public IPage<SysIndexPO> findPage(Integer pageIndex, Integer pageSize){
        LambdaQueryWrapper<SysIndexPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysIndexPO::getCreateTime);
        return getBaseMapper().selectPage(new Page<>(pageIndex, pageSize),lambdaQueryWrapper);
    }

    public boolean isExist(String sysCode, Long excludeId){
        LambdaQueryWrapper<SysIndexPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysIndexPO::getSysCode, sysCode);
        lambdaQueryWrapper.ne(excludeId !=null, SysIndexPO::getId, excludeId);
        return getBaseMapper().selectCount(lambdaQueryWrapper)>0? Boolean.TRUE: Boolean.FALSE;
    }
}
